package com.gydz.gydzbaseframeworkboot.BaseControl.controller;

import com.gydz.gydzbaseframeworkboot.Common.utils.page.PageParam;
import com.gydz.gydzbaseframeworkboot.Common.utils.page.PageUtils;
import com.gydz.gydzbaseframeworkboot.Common.utils.page.TableDataInfo;
import com.gydz.gydzbaseframeworkboot.Common.utils.result.ResultUtils;
import com.gydz.gydzbaseframeworkboot.BaseControl.dao.UserInfoMapper;
import com.gydz.gydzbaseframeworkboot.BaseControl.domain.UserInfo;
import com.gydz.gydzbaseframeworkboot.BaseControl.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    UserInfoMapper userInfoMapper;

    @GetMapping("/homePage")
    public ModelAndView userModelAndView(ModelAndView modelAndView) {
        modelAndView.setViewName("homePage/homePage");
        return modelAndView;
    }


    @ResponseBody
    @RequestMapping("/user/list")
    @PreAuthorize("hasAuthority('admin') or hasAuthority('test')" )
    public void getUserInfo(HttpServletRequest request , HttpServletResponse httpServletResponse, @ModelAttribute("userInfo") UserInfo userInfo) throws IOException {
        PageUtils.startPage(new PageParam(userInfo.getiDisplayStart(),userInfo.getiDisplayLength(),userInfo.getsSortDir(),userInfo.getsSortCol()));
        List<UserInfo> userInfoList = userInfoMapper.userList(userInfo);
        TableDataInfo tableDataInfo = PageUtils.getDataTable(userInfoList);

        ResultUtils result =  ResultUtils.succ(200,"获取用户信息成功",tableDataInfo);
        ResultUtils.responseResult(httpServletResponse,result);
    }

    @GetMapping("/")
    public ModelAndView index(ModelAndView modelAndView) {
        modelAndView.setViewName("index");


        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        String account = userDetails.getUsername();
        //取登录的用户名
        modelAndView.addObject("nickName",userInfoService.getByUserAccount(account).getUserName());
        return modelAndView;
    }

    @GetMapping("/login")
    public ModelAndView login(ModelAndView modelAndView) {
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @GetMapping("/404")
    public ModelAndView error_404(ModelAndView modelAndView) {
        modelAndView.setViewName("404");
        return modelAndView;
    }
}
